import random
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

def send_sms(phone,code):
    client = AcsClient('LTAIZIxqSbbnSVvc','uyAyjgXHUIdkCmtYR2shB6COhTJ318','cn-hangzhou')

    code = "{'code':%s}"%(code)
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('http')
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers',phone)
    request.add_query_param('SignName', 'yourpig')
    request.add_query_param('TemplateCode', "SMS_172220024")
    request.add_query_param('TemplateParam', code)

    response = client.do_action(request)
    print(code)
    return str(response,encoding='utf-8')

def get_code(n=6,alpha=True):
    s = ''
    for i in range(n):
        num = random.randint(0,9)
        if alpha:
            upper_alpha = chr(random.randint(65,90))
            lower_alpha = chr(random.randint(97,122))
            num = random.choice([num,upper_alpha,lower_alpha])
        s = s + str(num)
    return s


if __name__ == '__main__':
    send_sms(17634026167, get_code(6, False))